<%@ page import="com.mysql.cj.jdbc.ConnectionGroupManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="entity.other.File" %>
<%@ page import="org.apache.commons.dbutils.handlers.BeanListHandler" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.dbutils.*" %><%--
  Created by IntelliJ IDEA.
  User: 李学健
  Date: 2024/3/28
  Time: 23:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<%--style中的标签有先后顺序，需要先设置图片--%>
<body style=" background: url(../images/backguound.jpg) ;background-size: cover ; ">
<h1 style="color: #3880c2">
    <a href="index.jsp" style="color: crimson">返回登录</a>
</h1>
<%
    request.setCharacterEncoding("UTF-8");
    //获取用户权限,存入session
    int power = Integer.parseInt(request.getParameter("power"));
    session.setAttribute("power", power);

%>
<center style="color: #447bde">
    <h1>欢迎进入云文件管理系统!</h1>
    <%
        if (0 == power)
            out.write("<h2>您是游客，仅可查看公开文件</h2><br>");
        if (1 == power)
            out.write("<h2>您是用户，可查看公开文件与本人文件和上传文件</h2><br>");
    %>
    <%
        /*文件上传组件*/
        if (1 == power) {
    %>
    <%-- ☆:   加/是绝对路径，不加是相对路径 --%>
    <form action="UploadServlet" method="post" enctype="multipart/form-data">
        <font color="#447bde">文件权限:</font>
        <label>私有 <input type="radio" name="filePower" value="0"></label>
        <label>公开<input type="radio" name="filePower" value="1"></label>
        <br>
        上传文件:<input type="file" name="myFile"><br><br>
        <input type="submit" value="上传"><br><br>
    </form>
    <%
        }
    %>
    <%--用户，可以下载查看--%>
    <%
        //先声明fileList,全局变量
        if (1 == power) {
            List<File> fileList;
            try {
                //queryRunner查询公开文件
                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");
                QueryRunner queryRunner = new QueryRunner();
                //解决字段映射问题
                BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
                //解决字段映射问题
                fileList = queryRunner.query(connection, "select f.*,u.username from file f\n" +
                        "                               inner join user u\n" +
                        "                                          on u.id=f.user_id\n" +
                        //basicRowProcessor放在new BeanListHandler里面
                        "where power=1", new BeanListHandler<File>(File.class, basicRowProcessor));
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            for (File file : fileList) {
                out.write("<a href=\"DownloadServlet?filePath=" + file.getFilePath() + "\">" + file.getFileName()
                        + "</a>" + "    <a href=\"ReportServlet?fileId=" + file.getId() + "\" style=\"color: red;font-size: 10px\">举报</a>" +
                        "<br>上传用户 : " + file.getUserName() + "上传时间: " + file.getUpTime() + "下载量:" + file.getDownload() + "<br>");
            }
        }
    %>
    <%--游客，仅仅可以查看文件之类--%>
    <%--用<img>标签显示出图片--%>
    <%
        if (0 == power) {
            List<File> fileList;
            try {
                //queryRunner查询公开文件
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");
                QueryRunner queryRunner = new QueryRunner();
                //解决字段映射问题
                BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
                //解决字段映射问题
                fileList = queryRunner.query(connection, "select f.*,u.username from file f\n" +
                        "                               inner join user u\n" +
                        "                                          on u.id=f.user_id\n" +
                        //basicRowProcessor放在new BeanListHandler里面
                        "where power=1", new BeanListHandler<File>(File.class, basicRowProcessor));
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            for (File file : fileList) {
                out.write("<a" + file.getFilePath() + "\">" + file.getFileName()
                        + "</a><br>上传用户 : " + file.getUserName() + "上传时间: " + file.getUpTime() + "下载量:" + file.getDownload() + "<br>");
            }
        }
    %>
    <% if (1 == power) {%>
    <a href="myFile.jsp">我的文件</a>
    <%} %>

</center>
</body>
</html>

